package com.wiscamp.ninechapters.problems.adapter.resources.exams;


import com.wiscamp.ninechapters.problems.application.dto.commands.exams.CreatePaperPartCommand;
import com.wiscamp.ninechapters.problems.application.dto.commands.exams.CreatePaperSectionCommand;
import com.wiscamp.ninechapters.problems.application.dto.commands.exams.UpdatePaperPartCommand;
import com.wiscamp.ninechapters.problems.application.dto.commands.exams.UpdatePaperSectionCommand;
import com.wiscamp.ninechapters.problems.application.dto.vo.exams.PaperPartVO;
import com.wiscamp.ninechapters.problems.application.dto.vo.exams.PaperSectionVO;
import com.wiscamp.ninechapters.problems.application.services.PaperPartAppService;
import cube.ddd.adapter.BaseController;
import cube.ddd.adapter.RestResponse;
import cube.ddd.application.command.DeleteCommand;
import cube.ddd.aspect.HandleResourceException;
import cube.ddd.utils.ResourceUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.core.Context;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;

/**
 * 考试试卷端点
 *
 * @author yifeng
 */
@RestController
@RequestMapping("/api/Problems")

@Tag(name = "题库-考试试卷信息")
public class PaperPartController extends BaseController {

    private final PaperPartAppService paperPartAppService;

    public PaperPartController(PaperPartAppService paperPartAppService) {
        this.paperPartAppService = paperPartAppService;
    }

    /**
     * "通过考试编号获取考试试卷部分列表
     *
     * @return 试卷部分列表
     */
    @GetMapping("Exams/{examId}/PaperParts")
    @Operation(summary = "通过考试编号获取考试试卷部分列表")
    @HandleResourceException
    public RestResponse<Collection<PaperPartVO>> getPaperPartsByExam(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                     @PathVariable("examId") long examId) {
        return ResourceUtils.getListById(request, response, examId, paperPartAppService::getPaperPartsByExam);
    }

    /**
     * 通过试卷部分编号获取考试试卷部分
     *
     * @return 试卷部分
     */
    @GetMapping("PaperParts/{partId}")
    @Operation(summary = "通过试卷部分编号获取考试试卷部分")
    @HandleResourceException
    public RestResponse<PaperPartVO> getPart(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                             @PathVariable("partId") long partId) {
        return ResourceUtils.getById(request, response, partId, paperPartAppService::getPaperPart);
    }

    /**
     * 创建考试试卷部分
     *
     * @param command
     * @return
     */
    @PostMapping("PaperParts")
    @Operation(summary = "创建考试试卷部分")
    @HandleResourceException
    public RestResponse<PaperPartVO> createPaperPart(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                     @RequestBody CreatePaperPartCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, paperPartAppService::createPaperPart);
    }

    /**
     * 更新考试试卷部分
     *
     * @param command
     * @return
     */
    @PutMapping("PaperParts")
    @Operation(summary = "更新考试试卷部分")
    @HandleResourceException
    public RestResponse<PaperPartVO> updatePaperPart(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                     @RequestBody UpdatePaperPartCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, paperPartAppService::updatePaperPart);
    }

    /**
     * 通过试卷部分编号删除考试试卷部分
     *
     * @param partId
     * @return
     */
    @DeleteMapping("PaperParts")
    @Operation(summary = "通过试卷部分编号删除考试试卷部分")
    @HandleResourceException
    public RestResponse<Boolean> deletePaperPart(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                 @RequestBody DeleteCommand command) {
        return ResourceUtils.delete(request, response, command, paperPartAppService::deletePaperPart);
    }
    //endregion


    //region paper section

    /**
     * 通过考试试卷部分编号获取试卷节列表
     *
     * @return 试卷节列表
     */
    @GetMapping("PaperParts/{partId}/Sections")
    @Operation(summary = "通过考试试卷部分编号获取试卷节列表")
    @HandleResourceException
    public RestResponse<Collection<PaperSectionVO>> getPaperSectionsByPart(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                                           @PathVariable("partId") long partId) {
        return ResourceUtils.getListById(request, response, partId, paperPartAppService::getPaperSectionsByPart);
    }

    /**
     * 通过考试试卷节编号获取考试试卷节
     *
     * @return 试卷节
     */
    @GetMapping("PaperParts/{partId}/Sections/{sectionId}")
    @Operation(summary = "通过考试试卷节编号获取考试试卷节")
    @HandleResourceException
    public RestResponse<PaperSectionVO> getSection(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                   @PathVariable("sectionId") long sectionId) {
        return ResourceUtils.getById(request, response, sectionId, paperPartAppService::getPaperSection);
    }

    /**
     * 创建考试试卷节
     *
     * @param command
     * @return
     */
    @PostMapping("PaperParts/{partId}/Sections")
    @Operation(summary = "创建考试试卷节")
    @HandleResourceException
    public RestResponse<PaperSectionVO> createPaperSection(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                           @RequestBody CreatePaperSectionCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, paperPartAppService::createPaperSection);
    }

    /**
     * 更新考试试卷节
     *
     * @param command
     * @return
     */
    @PutMapping("PaperParts/{partId}/Sections")
    @Operation(summary = "更新考试试卷节")
    @HandleResourceException
    public RestResponse<PaperSectionVO> updatePaperSection(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                           @RequestBody UpdatePaperSectionCommand command) {
        return ResourceUtils.createOrUpdate(request, response, command, paperPartAppService::updatePaperSection);
    }

    /**
     * 通过试卷节编号删除考试试卷节
     *
     * @param sectionId
     * @return
     */
    @DeleteMapping("PaperParts/{partId}/Sections")
    @Operation(summary = "通过试卷节编号删除考试试卷节")
    @HandleResourceException
    public RestResponse<Boolean> deletePaperSection(@Context HttpServletRequest request, @Context HttpServletResponse response,
                                                    @RequestBody DeleteCommand command) {
        return ResourceUtils.delete(request, response, command, paperPartAppService::deletePaperSection);
    }
}
